package org.opens.lambda.collector;

import org.junit.Test;
import org.opens.lambda.UserEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class EmptyListToStream {

    /**
     * 说明:
     *      1. 如果转换的list为空, 则stream语法不会执行, 所以也不会抛出异常.
     */
    @Test
    public void test1() {
        List<UserEntity> list = new ArrayList<>();
        List<UserEntity> collect = list.stream().peek(x -> x.setAge(1)).collect(Collectors.toList());
        System.out.println(collect);
    }

    /**
     * 说明:
     *      1. 空集合通过stream转换map也不会报错.
     */
    @Test
    public void test2() {
        List<UserEntity> list = new ArrayList<>();
        Map<Integer, String> collect = list.stream().peek(x -> x.setAge(1)).collect(Collectors.toMap(UserEntity::getAge, UserEntity::getName));
        System.out.println(collect);
    }

}
